﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace ThuPhiVeSinh
{
    public partial class frmPhanBoKhachHang : DevExpress.XtraEditors.XtraForm
    {
        DataClasses_TPVSDataContext dc = new DataClasses_TPVSDataContext();
        string GroupConTrol2Text = "Danh sách khách hàng phân bổ cho nhân viên: ";

        public frmPhanBoKhachHang()
        {
            InitializeComponent();
        }

        //Get Source Danh Muc
        #region

        private Object QuanHuyen()
        {
            var source = dc.GetTable<Quan_Huyen>();
            return source;
        }

        private Object PhuongXa()
        {
            var source = dc.GetTable<Phuong_Xa>();
            return source;
        }

        private Object KhuVuc()
        {
            var source = dc.GetTable<Khu_Vuc>();
            return source;
        }

        private Object Duong()
        {
            var source = dc.GetTable<Duong>();
            return source;
        }

        private Object LoaiKhachHang()
        {
            var source = dc.GetTable<Loai_Khach_Hang>();
            return source;
        }
        #endregion

        //GridControl 1
        #region

        private Object GetKHsWithOutEmployee()
        {
            var khs = from kh in dc.Khach_Hangs where kh.KH_NVID == null select kh;
            return khs;
        }

        private void FillData2GridConTrol1()
        {           
            gridControl1.DataSource = GetKHsWithOutEmployee();

            FillSource2LookUpEdit(cbbQuanHuyen1, QuanHuyen(), "QH_Ten", "QH_ID");
            FillSource2LookUpEdit(cbbPhuongXa1, PhuongXa(), "PX_Ten", "PX_ID");
            FillSource2LookUpEdit(cbbKhuVuc1, KhuVuc(), "KV_Ten", "KV_ID");
            FillSource2LookUpEdit(cbbTuyenDuong1, Duong(), "D_Ten", "D_ID");
            FillSource2LookUpEdit(cbbLoaiKH1, LoaiKhachHang(), "L_TenLoai", "L_ID");

        }
        #endregion

        //GridControl 2
        #region
        private Object GetKHs()
        {
            var khs = from kh in dc.Khach_Hangs where kh.KH_NVID == (int)cbbNhanVien.EditValue select kh;
            return khs;
        }

        private void FillData2GridConTrol2()
        {
            if (cbbNhanVien.EditValue != null)
            {
                gridControl2.DataSource = GetKHs();

                FillSource2LookUpEdit(cbbQuanHuyen2, QuanHuyen(), "QH_Ten", "QH_ID");
                FillSource2LookUpEdit(cbbPhuongXa2, PhuongXa(), "PX_Ten", "PX_ID");
                FillSource2LookUpEdit(cbbKhuVuc2, KhuVuc(), "KV_Ten", "KV_ID");
                FillSource2LookUpEdit(cbbTuyenDuong2, Duong(), "D_Ten", "D_ID");
                FillSource2LookUpEdit(cbbLoaiKH2, LoaiKhachHang(), "L_TenLoai", "L_ID");

            }

        }
        #endregion

        //Test Null cbbNhanVien
        private Boolean cbbIsNull()
        {
            Boolean rs = true;
            if (cbbNhanVien.EditValue != null)
            {
                rs = false;
            }
            return rs;
        }

        private void FillSource2LookUpEdit(DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit cbb, object source, string DisplayM, string ValueM)
        {

            cbb.Properties.DataSource = source;
            cbb.Properties.DisplayMember = DisplayM;
            cbb.Properties.ValueMember = ValueM;
        }

        private void frmPhanBoKhachHang_Load(object sender, EventArgs e)
        {
            var nvs = from nv in dc.Nhan_Viens select nv;
            cbbNhanVien.Properties.DataSource = nvs;
            cbbNhanVien.Properties.DisplayMember = "NV_MS";
            cbbNhanVien.Properties.ValueMember = "NV_ID";

            groupControl2.Text = GroupConTrol2Text;

            FillData2GridConTrol1();

            btAddAll.Enabled = false;
            btRemoveAll.Enabled = false;
        }

        private void cbbNhanVien_EditValueChanged(object sender, EventArgs e)
        {
            if (cbbNhanVien.EditValue != null)
            {
                var nv = (from _nv in dc.Nhan_Viens where _nv.NV_ID == (int)cbbNhanVien.EditValue select _nv).Single();
                txtHoTenNV.Text = nv.NV_Ten;
                txtSDT.Text = nv.NV_DienThoai;
                string tien;
                if (nv.NV_TienKyQuy != null)
                {
                    tien = string.Format("{0:F0}", nv.NV_TienKyQuy.Value);
                }
                else
                {
                    tien = "0";
                }

                txtTienKyQuy.Text = tien;

                groupControl2.Text = GroupConTrol2Text + nv.NV_Ten.ToUpper();

                FillData2GridConTrol2();
            }
            
        }

        private void btAddAll_Click(object sender, EventArgs e)
        {
            int[] selectedrows = gridView1.GetSelectedRows();
            
            if (!cbbIsNull())
            {
                
                
                Nhan_Vien nv = (from n in dc.Nhan_Viens where n.NV_ID == (int)cbbNhanVien.EditValue select n).Single();
                foreach (int i in selectedrows)
                {
                    if (i>=0)
                    {
                        Khach_Hang kh = (Khach_Hang)gridView1.GetRow(i);
                        kh.Nhan_Vien = nv;
                    }
                }

                dc.SubmitChanges();

                FillData2GridConTrol1();
                FillData2GridConTrol2();
            }
            else
            {
                XtraMessageBox.Show("Vui lòng chọn nhân viên", "Yêu cầu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btRemoveAll_Click(object sender, EventArgs e)
        {
            int[] selectedrows = gridView2.GetSelectedRows();
                        
            foreach (int i in selectedrows)
            {
                if (i >= 0)
                {
                    Khach_Hang kh = (Khach_Hang)gridView2.GetRow(i);
                    kh.Nhan_Vien = null;
                }
            }

            dc.SubmitChanges();

            FillData2GridConTrol2();
            FillData2GridConTrol1();
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column == colSTT1)
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }

        private void gridView2_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column == colSTT2)
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }

        private void gridView2_GotFocus(object sender, EventArgs e)
        {
            btRemoveAll.Enabled = true;
            btAddAll.Enabled = false;
        }

        private void gridView1_GotFocus(object sender, EventArgs e)
        {
            btRemoveAll.Enabled = false;
            btAddAll.Enabled = true;
        }
   
    }
}